package com.taptech.doufu.model.user

import android.app.Activity
import com.taptech.doufu.model.novel.bean.NovelAttentionBean
import com.taptech.doufu.net.retrofit.api.ApiClient
import com.taptech.doufu.net.retrofit.api.RxJavaHelper
import rx.Observer

class UserDetailModel(val activity: Activity) {

    fun getUserDetail(uid:Int,observer: Observer<UserDetailBean>) {
        ApiClient.getInstance().setUseCache(false).service.getUserDetail(uid)
                .map {
                    if (it != null) {
                        it.data
                    }else{
                        UserDetailBean()
                    }
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun getUserNovelList(uid: Int, observer: Observer<UserDetailBean>) {
        ApiClient.getInstance().setUseCache(false).service.getUserNovelList(uid)
                .map {
                    if (it != null) {
                        it.data
                    }else{
                        UserDetailBean()
                    }
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun getUserPoster(uid: Int,observer:Observer<UserPosterBean>) {
        ApiClient.getInstance().setUseCache(false).service.getUserPoster(uid)
                .map {
                    if (it != null) {
                        it.data
                    }else{
                        UserPosterBean()
                    }
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }


    fun attentionUser(userId: Int,observer:Observer<NovelAttentionBean>) {
        ApiClient.getInstance().service.attentionUser(userId)
                .map {
                    it.data
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun cancelAttentionUser(userId: Int,observer: Observer<Boolean>){
        ApiClient.getInstance().service.cancelAttentionUser(userId)
                .map {
                    it.data == "ok"
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }
}